package com.demo.apparel.controller.web;

import com.alibaba.excel.EasyExcel;
import com.demo.apparel.common.Result;
import com.demo.apparel.dto.web.AnaQueryDTO;
import com.demo.apparel.dto.web.IdListDTO;
import com.demo.apparel.entity.Ana;
import com.demo.apparel.service.web.AnaService;
import com.demo.apparel.vo.PageVO;
import com.demo.apparel.vo.web.AnaImportVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/web/ana")
@Tag(name = "语录信息接口", description = "语录信息")
public class AnaController {

    @Autowired
    private AnaService anaService;

    @Operation(summary = "获取全部语录")
    @GetMapping("/list")
    public Result<List<Ana>> list() {
        return Result.success(anaService.getAll());
    }

    @Operation(summary = "新增语录")
    @PostMapping("/add")
    public Result<?> add(@RequestBody Ana ana) {
        anaService.add(ana);
        return Result.success("新增成功");
    }

    @Operation(summary = "删除语录（单个）")
    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable String id) {
        anaService.delete(id);
        return Result.success("删除成功");
    }

    @Operation(summary = "分页查询语录（关键词）")
    @PostMapping("/list")
    public Result<PageVO<Ana>> list(@RequestBody AnaQueryDTO dto) {
        PageVO<Ana> page = anaService.getPage(dto.getKeyword(), dto.getPageNum(), dto.getPageSize());
        return Result.success(page);
    }

    @Operation(summary = "批量删除语录")
    @PostMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestBody IdListDTO dto) {
        anaService.deleteBatch(dto.getId());
        return Result.success("批量删除成功");
    }

    @Operation(summary = "导出语录为 Excel")
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        anaService.exportAll(response);
    }

    @Operation(summary = "编辑语录")
    @PutMapping("/update")
    public Result<?> update(@RequestBody Ana ana) {
        anaService.update(ana);
        return Result.success("修改成功");
    }




    @GetMapping("/template")
    @Operation(summary = "导入语录模块")
    public void downloadTemplate(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        String fileName = URLEncoder.encode("语录导入模板", StandardCharsets.UTF_8);
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        List<AnaImportVO> templateList = new ArrayList<>();
        // 放一行空数据用于示例展示
        templateList.add(new AnaImportVO());

        EasyExcel.write(response.getOutputStream(), AnaImportVO.class)
                .sheet("模板")
                .doWrite(templateList);
    }

    @PostMapping(value = "/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "导入语录Excel")
    public Result<?> importAna(
            @Parameter(
                    description = "上传Excel文件",
                    required = true,
                    content = @Content(
                            mediaType = MediaType.MULTIPART_FORM_DATA_VALUE,
                            schema = @Schema(type = "string", format = "binary")
                    )
            )
            @RequestParam("file") MultipartFile file,
            @Parameter(description = "是否覆盖", required = false)
            @RequestParam(value = "isCover", required = false) Boolean isCover
    ) throws Exception {
        anaService.importAna(file);
        // 业务代码
        return Result.success("导入成功");
    }
}
